// pages/socks/socks.js
const app = getApp()
var websocket = require('../../../util/websocket');
// var utils = require('../../utils/util.js');
Page({
 /**
 * 页面的初始数据
 */
 data: {
 newslist: [],
 userInfo: {},
 scrollTop: 0,
 increase: false,//图片添加区域隐藏
 aniStyle: true,//动画效果
 message: "",
 previewImgList: [],
 toUser:'',
 toUserId:''
 },
 
	/**
	 * 生命周期函数--监听页面加载
	 */
     onLoad: function(options) {
        var that = this
        that.setData({
            userInfo : wx.getStorageSync('userInfo'),
            toUser:options.toUser,
            toUserId:options.toUserId
        })
            // const userInfo = wx.getStorageSync('userInfo')
            console.log(this.data.userInfo)
            console.log("to",this.data.toUser)
        //调通接口
        websocket.connect(this.data.userInfo, function (res) {
            console.log(JSON.parse("res",res.data))
            var list = []
            list = that.data.newslist
            list.push(res.data)
            that.setData({
            newslist: list
            })
        })
        },
        
        send: function() {
            var flag = this
            if (this.data.content.trim() == "") {
                wx.showToast({
                    title: '消息不能为空哦~',
                    icon: "none",
                    duration: 2000
                })
            } else {
                setTimeout(function() {
                    flag.setData({
                        increase: false
                    })
                }, 500)
                websocket.send('{ "content": "' + this.data.content + '","msgType":"text", "fromUser": "' + this
                    .data.userInfo.nick_name  + '", "avatarUrl": "' + this.data.userInfo.avatar + '", "fromUserId": "' + this.data.userInfo.user_id + '" , "toUser": "' + this.
                    data.toUser + '" , "toUserId": "' + this.data.toUserId + '", "sendDate": "' + Date.parse(new Date()) + '" }')
                    this.bottom()
                    // this.cleanInput()
            }
        },
    
        //监听input值的改变
        bindChange(res) {
            this.setData({
                content: res.detail.value
            })
        },
        cleanInput() {
            //button会自动清空，所以不能再次清空而是应该给他设置目前的input值
            this.setData({
                content: this.data.content
            })
        },
        increase() {
            this.setData({
                increase: true,
                aniStyle: true
            })
        },
        //点击空白隐藏message下选框
        outbtn() {
            this.setData({
                increase: false,
                aniStyle: true
            })
        },
        //聊天消息始终显示最底端
        bottom: function() {
            var query = wx.createSelectorQuery()
            query.select('#flag').boundingClientRect()
            query.selectViewport().scrollOffset()
            query.exec(function(res) {
                wx.pageScrollTo({
                    scrollTop: res[0].bottom // #the-id节点的下边界坐标
                })
                res[1].scrollTop // 显示区域的竖直滚动位置
            })
        },
        /**
         * 生命周期函数--监听页面初次渲染完成
         */
        onReady: function() {
    
        },
    
        /**
         * 生命周期函数--监听页面显示
         */
        onShow: function() {
    
        },
    
        /**
         * 生命周期函数--监听页面隐藏
         */
        onHide: function() {
    
        },
    
        /**
         * 生命周期函数--监听页面卸载
         */
        onUnload: function() {
            wx.closeSocket();
            wx.showToast({
                title: '连接已断开~',
                icon: "none",
                duration: 2000
            })
        },
    
        /**
         * 页面相关事件处理函数--监听用户下拉动作
         */
        onPullDownRefresh: function() {
    
        },
    
        /**
         * 页面上拉触底事件的处理函数
         */
        onReachBottom: function() {
    
        },
    
        /**
         * 用户点击右上角分享
         */
        onShareAppMessage: function() {
    
        }
    })
    